<?php

namespace Myth\Data\Model\Mapper;

abstract class AMapper implements IMapper
{
    protected $_namespace;
    protected $_name;
    protected $_mappings;
    
    public function find($where = null, $order = null, $count = null, $offset = null, $recursion = 1)
    {
        $select = $this->getSelect($this->getRelatedModels($recursion));
    }
    
    protected function getSelect($models) 
    {
        //get an instance of al the necessary mappers
        //get all necessary DbTables 
        //join the all in an appropriate fashion
    }
    
    protected function getFieldData($name, $asArray = false)
    {
        $data = array(
            'ns' => $this->_getNamespace(),
            'name' => $this->_getName()
        );
        switch (substr_count($name, '.')) {
            case 0:
                $data['prop'] = $name;
            case 1:
                $pieces = explode('.', $name);
                $data['name'] = $pieces[0];
                $data['prop'] = $pieces[1];
            case 2:
                $pieces = explode('.', $name);
                $data['ns'] = $pieces[0];
                $data['name'] = $pieces[1];
                $data['prop'] = $pieces[2];
            default:
                throw new \Exception('Invalid field formatting');
        }
        if (!$asArray) return $data['ns'].'.'.$data['name'].'.'.$data['prop'];
        return $data;
    }
    
    protected function getRelatedModels($recursion = 1)
    {
        //get all related models
        $root = array_keys($this->_mappings);
        $children = false;
        if (array_key_exists('Relations', $this->_mappings)) {
            if ($recursion > 1) {
                foreach ($children as $child) {
                    $mapper = $this->getMapper($this->getFieldData($child));
                    $children[] = $mapper->getRelatedModels(--$recursion);
                }
            } else {
                $children = array_keys($this->_mappings['Relations']);
            }
        }
        if (false !== $children) $root[] = $children;
        return $root;
    }
    

	public function save($m, $tables = null)
    {
        
    }
    
    protected function _buildSelect()
    {
        
    }
}